package com.example.springboot.service.Impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.common.ResultCode;
import com.example.springboot.common.config.JwtTokenUtils;
import com.example.springboot.dao.TakeOffMapper;
import com.example.springboot.domain.po.Account;
import com.example.springboot.domain.po.TakeOff;
import com.example.springboot.exception.CustomException;
import com.example.springboot.service.TakeOffService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author：伐柯梦姝
 * @Package：com.example.springboot.service.Impl
 * @Project：springboot
 * @name：TakeOffServiceImpl
 * @Date：2025/2/24 22:00
 * @Filename：TakeOffServiceImpl
 * @for:
 */
@Service
public class TakeOffServiceImpl extends ServiceImpl<TakeOffMapper, TakeOff> implements TakeOffService {
       @Resource
       private TakeOffMapper takeOffMapper;

       @Override
       public List<TakeOff> GetAll(){
              return this.lambdaQuery().list();
       }

       @Override
       public TakeOff takeOffRegister(TakeOff takeOff) {
              takeOffMapper.insert(takeOff);
              return takeOff;
       }

       @Override
       public TakeOff findById(Integer id) {
              return takeOffMapper.selectById(id);
       }

       @Override
       public void delete(Integer id) {
              takeOffMapper.deleteById(id);
       }

       @Override
       public PageInfo<TakeOff> findPage(TakeOff search, Integer pageNum, Integer pageSize) {
              Account user = JwtTokenUtils.getCurrentUser();
              if (ObjectUtil.isEmpty(user)) {
                     throw new CustomException(ResultCode.USER_NOT_LOGIN);
              }
              PageHelper.startPage(pageNum, pageSize);
              List<TakeOff> all = findByCondition(search,user);

              return PageInfo.of(all);
       }
       @Override
       public List<TakeOff> findByCondition(TakeOff search, Account user) {
              if (1 == user.getRole()){
                     return takeOffMapper.findBySearch(search,null,null);
              } else {
                     return takeOffMapper.findBySearch(search, user.getId(), user.getRole());
              }
       }


       @Override
       public TakeOff takeOffEdit(TakeOff takeOff) {
              takeOffMapper.updateById(takeOff);
              return takeOff;
       }
}
